package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.*;
import com.drug.service.PurchasementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PurchasementServiceImpl implements PurchasementService {

    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private PodiPdMapper podiPdMapper;
    @Autowired
    private PutInStorageMapper putInStorageMapper;
    @Autowired
    private DirgRgMapper dirgRgMapper;
    @Autowired
    private StatementMapper statementMapper;

    @Override
    public Result findAllSupplier() {
        Result result = new Result<>();
        List<Supplier> suppliers = supplierMapper.selectAll();
        result.setData(suppliers);
        return result;
    }

    /**
     * 多条件查询采购单
     * @return
     */
    @Override
    public Result findAllPM(Map map, Integer page) {
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        //采购单
        PurchaseOrder purchaseOrder1 = new PurchaseOrder();
        String poNumber =(String) map.get("dinumber");
        if (poNumber==null||poNumber.equals("")){
            poNumber=null;
        }
        String cname =(String)map.get("cname");
        if (poNumber==null||poNumber.equals("")){
            poNumber=null;
        }
        String cengci = (String) map.get("cengci");
        if (cengci==null||cengci.equals("")||cengci.equals("全部")){
            cengci=null;
        }else {
           // purchaseOrder.setPoState((cengci));
        }
        String lefPprice = (String) map.get("lefPprice");
        if (lefPprice==null||lefPprice.equals("")){
            lefPprice=null;
        }
        String rightPrice = (String) map.get("rightPrice");
        if (rightPrice==null||rightPrice.equals("")){
            rightPrice=null;
        }
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoNumber(poNumber);
        purchaseOrder.setPoName(cname);
        purchaseOrder.setPoState(cengci);
        List<PurchaseOrder> selectAll = purchaseOrderMapper.selectAll(purchaseOrder,lefPprice,rightPrice);
        PageInfo<PurchaseOrder> pageInfo= new PageInfo<PurchaseOrder>(selectAll);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 添加采购表
     * @param map
     * @return
     */
    @Override
    public Result addpurchase(Map map) {

        String  hName = (String) map.get("hName");

        //根据医院名字查id
        Hospital hospital = new Hospital();
        Integer hId = hospitalMapper.selectByName(hName);
        String  poContacts = (String) map.get("poContacts");

        String  poName = (String) map.get("poName");

        String  poPhone = (String) map.get("poPhone");

        String   poRemarks = (String) map.get("poRemarks");

        String   poSupplyTime = (String) map.get("poSupplyTime");

        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoUId(hId);
        purchaseOrder.setPoContacts(poContacts);
        purchaseOrder.setPoName(poName);
        purchaseOrder.setPoPhone(poPhone);
        purchaseOrder.setPoRemarks(poRemarks);
        purchaseOrder.setPoSupplyTime(null);
        purchaseOrder.setPoNumber(null);
        purchaseOrder.setPoAuditor(null);
        purchaseOrder.setPoAuditOpinion(null);
        purchaseOrder.setPoAuditTime(null);
        purchaseOrder.setPoCreateTime(null);
        purchaseOrder.setPoSubmissionTime(null);



        purchaseOrderMapper.addAll(purchaseOrder);
        Result result = new Result<>();
        result.setMsg("成功");
        return result;
    }

    /**
     * 根据id查询采购表信息
     * @param id
     * @return
     */
    @Override
    public Result delectById(Integer id) {
        Result result = new Result<>();
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoId(id);
        purchaseOrderMapper.deleteByPrimaryKey(purchaseOrder);
        result.setMsg("成功");
        return result;
    }

    /**
     * 根据id查询采购表信息
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        Result result = new Result<>();
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoId(id);
        List<PurchaseOrder> list = purchaseOrderMapper.selectById(purchaseOrder);
        result.setData(list);
        return result;
    }

    /**
     * 修改采购表
     * @param map
     * @return
     */
    @Override
    public Result updatalala(Map map) {
        Result result = new Result<>();
        String id =(String) map.get("poid");

        String  hName = (String) map.get("hName");

        //根据医院名字查id
        Hospital hospital = new Hospital();
        Integer hId = hospitalMapper.selectByName(hName);
        String  poContacts = (String) map.get("poContacts");

        String  poName = (String) map.get("poName");

        String  poPhone = (String) map.get("poPhone");

        String   poRemarks = (String) map.get("poRemarks");

        String   poSupplyTime = (String) map.get("poSupplyTime");

        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setPoId(Integer.parseInt(id));
        purchaseOrder.setPoUId(hId);
        purchaseOrder.setPoContacts(poContacts);
        purchaseOrder.setPoName(poName);
        purchaseOrder.setPoPhone(poPhone);
        purchaseOrder.setPoRemarks(poRemarks);

        purchaseOrderMapper.uptadelalala(purchaseOrder);
        result.setMsg("成功");
        return result;
    }

    /**
     * 查询采购单全部信息
     * @param map
     * @return
     */
    @Override
    public Result findAllla(Map map, Integer page) {
        //使用分页插件
        PageHelper.startPage(page, 5);
        Result result = new Result();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //药品质量层次
        DrugQualityLevel drugQualityLevel = new DrugQualityLevel();
        //供货商
        Supplier supplier = new Supplier();
        //中间表
        PodiPd podiPd = new PodiPd();

        String number = (String) map.get("dinumber");
        if (number == null || number.equals("")) {
            number = null;
        }
        String diName = (String) map.get("cname");
        if (diName == null || diName.equals("")) {
            diName = null;
        }
        String diForm = (String) map.get("jixing");
        if (diForm == null || diForm.equals("")) {
            diForm = null;
        }
        String diSpecifications = (String) map.get("guige");
        if (diSpecifications == null || diSpecifications.equals("")) {
            diSpecifications = null;
        }
        String diUnit = (String) map.get("danwei");
        if (diUnit == null || diUnit.equals("")) {
            diUnit = null;
        }
        String diCc = (String) map.get("cc");
        if (diCc == null || diCc.equals("")) {
            diCc = null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye == null || qiye.equals("")) {
            qiye = null;
        } else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin == null || shangpin.equals("")) {
            shangpin = null;
        } else {
            drugAtegory.setDaName(shangpin);
        }

        String lefPprice = (String) map.get("lefPprice");
        if (lefPprice == null || lefPprice.equals("")) {
            lefPprice = null;
        }
        String rightPrice = (String) map.get("rightPrice");
        if (rightPrice == null || rightPrice.equals("")) {
            rightPrice = null;
        }
        //质量层次
        String cengci = (String) map.get("cengci");
        if (cengci == null || cengci.equals("") || cengci.equals("0")) {
            cengci = null;
        } else {
            drugQualityLevel.setDqlId(Integer.parseInt(cengci));
        }
        String leibie = (String) map.get("leibie");
        if (leibie == null || leibie.equals("") || leibie.equals("0")) {
            leibie = null;
        } else {
            drugCategory.setDcName(leibie);
        }
        String state = (String) map.get("state");
        if (state == null || state.equals("")) {
            state = null;
        }
        String sup = (String) map.get("dcid");
        if (state == null || state.equals("0")) {
            state = null;
        } else {
            supplier.setSurId(Integer.parseInt(sup));
        }
        String id = (String) map.get("id");
        if (id==null || id.equals("")){
            id=null;
        }else {
            podiPd.setPodiPoid(Integer.parseInt(id));
        }
        //药品信息
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(number);
        drugInformation.setDiName(diName);
        drugInformation.setDiForm(diForm);
        drugInformation.setDiSpecifications(diSpecifications);
        drugInformation.setDiUnit(diUnit);
        drugInformation.setDiCc(diCc);
        drugInformation.setDiTradingStatus(state);
        drugInformation.setDiIsdel(0);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setDrugCategory(drugCategory);
        drugInformation.setDrugQualityLevel(drugQualityLevel);
        drugInformation.setSupplier(supplier);
        drugInformation.setPodiPd(podiPd);

        //多条件查询
        List<DrugInformation> drugInformations = drugInformationMapper.selectAllDrug2(drugInformation, lefPprice, rightPrice);
        PageInfo<DrugInformation> pageInfo = new PageInfo<DrugInformation>(drugInformations);
        result.setData(pageInfo);
        return result;

    }

    /**
     * 修改信息表里的供货商
     * @param
     * @return
     */
    @Override
    public Result updateghs(Map map) {
        Result result = new Result<>();
        PodiPd podiPd = new PodiPd();
        String v =(String) map.get("v");
        String id =(String) map.get("id");
        String b = (String) map.get("b");
        podiPd.setPodiPoid(Integer.parseInt(v));
        podiPd.setPodiDiid(Integer.parseInt(id));
        podiPd.setPodiSurid(Integer.parseInt(b));
        podiPdMapper.updateghs(podiPd);
        result.setMsg("yes");
        return result;
    }


    /**
     * 多条件查询入库表信息
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findAllrkb(Map map, Integer page) {
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //供货商
        Supplier supplier = new Supplier();
        //采购单
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //中间表
        PodiPd podiPd = new PodiPd();
        //根据医院名字查id
        Hospital hospital = new Hospital();
       // Integer hId = hospitalMapper.selectByName(hName);

        //采购单编号
        String ponumber = (String) map.get("ponumber");
        if (ponumber==null || ponumber.equals("")){
            ponumber=null;
        }else {
            purchaseOrder.setPoNumber(ponumber);
        }
        //采购单名称
        String poname = (String) map.get("poname");
        if (poname==null || poname.equals("")){
            poname=null;
        }else {
            purchaseOrder.setPoName(poname);
        }
        //采购状态
        String postate = (String) map.get("postate");
        if (poname==null || poname.equals("")){
            poname=null;
        }else {
            podiPd.setPodiState(postate);
        }
        //	采购医院
        String podihid = (String) map.get("podihid");
        if (podihid==null || podihid.equals("")){
            podihid=null;
        }else {
            Integer hId = hospitalMapper.selectByName(podihid);
            podiPd.setPodiHid(hId);
        }
        //流水号：
        String dinumber = (String) map.get("dinumber");
        if (dinumber==null || dinumber.equals("")){
            dinumber=null;
        }

        //通用名
        String cname = (String) map.get("cname");
        if (cname==null || cname.equals("")){
            cname=null;
        }
        String jixing1 = (String) map.get("jixing1");
        if (jixing1==null || jixing1.equals("")){
            jixing1=null;
        }
        String guige1 = (String) map.get("guige1");
        if (guige1==null || guige1.equals("")){
            guige1=null;
        }
        String danwei1 = (String) map.get("danwei1");
        if (danwei1==null || danwei1.equals("")){
            danwei1=null;
        }
        String ccc = (String) map.get("ccc");
        if (ccc==null || ccc.equals("")){
            ccc=null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye==null||qiye.equals("")){
            qiye=null;
        }else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin==null||shangpin.equals("")){
            shangpin=null;
        }else {
            drugAtegory.setDaName(shangpin);
        }
        String rukutime = (String) map.get("rukutime");
        if (rukutime==null || rukutime.equals("")){
            rukutime=null;
        }
        String rukutime1 = (String) map.get("rukutime1");
        if (rukutime1==null || rukutime1.equals("")){
            rukutime1=null;
        }
        String dcid1 = (String) map.get("dcid1");
        if (dcid1==null || dcid1.equals("0")){
            dcid1=null;
        }else {
            drugCategory.setDcName(dcid1);
        }
        String diapprovalnumber = (String) map.get("diapprovalnumber");
        if (diapprovalnumber==null || diapprovalnumber.equals("")){
            diapprovalnumber=null;
        }
        String rukudanhao = (String) map.get("rukudanhao");
        if (rukudanhao==null || rukudanhao.equals("")){
            rukudanhao=null;
        }
        String ssupplier = (String) map.get("ssupplier");
        if (ssupplier==null || ssupplier.equals("0")){
            ssupplier=null;
        }else {
            supplier.setSurId(Integer.parseInt(ssupplier));
        }
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setPurchaseOrder(purchaseOrder);
        drugInformation.setPodiPd(podiPd);
        drugInformation.setDiNumer(dinumber);
        drugInformation.setDiNumer(cname);
        drugInformation.setDiNumer(jixing1);
        drugInformation.setDiNumer(guige1);
        drugInformation.setDiNumer(danwei1);
        drugInformation.setDiNumer(ccc);
        drugInformation.setDrugAtegory(drugAtegory);
        drugInformation.setSupplier(supplier);

        List<DrugInformation> drugInformations = drugInformationMapper.selectPs(drugInformation);
        PageInfo<DrugInformation> pageInfo = new PageInfo<>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 添加入库表数据
     * @param map
     * @param
     * @return
     */
    @Override
    public Result addruku(Map map) {
        String psId = (String) map.get("id");
        String a = (String) map.get("a");
        String b = (String) map.get("b");
        String c = (String) map.get("c");
        String d = (String) map.get("d");
        String e = (String) map.get("e");

        PutInStorage putInStorage = new PutInStorage();
        String[] split = psId.split(",");
        List<String> diidList = new ArrayList<>();
        List<String> podiId = new ArrayList<>();
        for (String s : split) {
            String str=s.substring(0, s.indexOf("-"));
            diidList.add(str);
            String bb=s.substring(str.length()+1, s.length());
            podiId.add(bb);
        }
        for (int i = 0; i < diidList.size(); i++) {
            putInStorage.setPsdiPoid(Integer.parseInt(diidList.get(i)));
        }

        if (a!=""){
            putInStorage.setPsAmount(Integer.parseInt(a));
        }
        putInStorage.setPsNumber(b);
        putInStorage.setPsPihao(c);

        if (d!=""){
            putInStorage.setPsUsefulLife(Integer.parseInt(d));
        }
        if (e!=""){
            putInStorage.setPsdiPoid(Integer.parseInt(e));
        }
        putInStorageMapper.addAll(putInStorage);
        for (int i = 0; i < podiId.size(); i++) {
            podiPdMapper.updateRuku(Integer.parseInt(podiId.get(i)));
        }
        Result result = new Result<>();
        result.setMsg("成功");
        return result;
    }

    @Override
    public List<DrugInformation> selectPs1() {
        List<DrugInformation> list = drugInformationMapper.selectPs1();
        return list;
    }

    /**
     * 查询所要添加的药品
     * @return
     */
    @Override
    public Result findyaopin() {
        List<DrugInformation> list = drugInformationMapper.selectyaopin();
        Result result = new Result<>();
        result.setData(list);
        return result;
    }

    /**
     * 向采购单中间表添加药品id
     * @param
     * @return
     */
    @Override
    public Result addyaopinid(Map map) {
        //药品id
        String diid = (String) map.get("id");
        //采购单id
        String poid = (String) map.get("v");
        //根据采购单id查询医院id
        Integer yiyuanid = purchaseOrderMapper.selectyiyuanid(Integer.parseInt(poid));
        //根据药品id查询采购商id
        Integer cgsid = drugInformationMapper.selectcaigoushangid(Integer.parseInt(diid));
        PodiPd podiPd = new PodiPd();
        podiPd.setPodiDiid(Integer.parseInt(diid));
        podiPd.setPodiPoid(Integer.parseInt(poid));
        podiPd.setPodiHid(yiyuanid);
        podiPd.setPodiSurid(cgsid);
        drugInformationMapper.updateDiProcurement(Integer.parseInt(diid));
        podiPdMapper.addyaopinid(podiPd);
        Result result = new Result<>();
        result.setMsg("yes");
        return result;
    }

    /**
     *向退货单中间表添加药品
     * @param map
     * @return
     */
    @Override
    public Result addyaopinid1(Map map) {
        //药品id
        String diid = (String) map.get("id");
        //采购单id
        String poid = (String) map.get("v");
        DirgRg dirgRg = new DirgRg();
        dirgRg.setDirgDiid(Integer.parseInt(diid));
        dirgRg.setDirgRgid(Integer.parseInt(poid));
        dirgRgMapper.addyaopin(dirgRg);
        Result result = new Result<>();
        result.setMsg("yes");
        return result;
    }

    /**
     * 添加结算单
     * @param map
     * @return
     */
    @Override
    public Result addjiesuandan(Map map) {

        String  hName = (String) map.get("hName");

        //根据医院名字查id
        Hospital hospital = new Hospital();
        Integer hId = hospitalMapper.selectByName(hName);
        String  poContacts = (String) map.get("poContacts");

        String  poName = (String) map.get("poName");

        String  poPhone = (String) map.get("poPhone");

        String   poRemarks = (String) map.get("poRemarks");

        String   poSupplyTime = (String) map.get("poSupplyTime");

        Statement statement = new Statement();
        statement.setSUName(hId);
        statement.setSContacts(poContacts);
        statement.setSName(poName);
        statement.setSPhone(poPhone);
        statement.setSRemarks(poRemarks);
        statementMapper.addjiesuandan(statement);
        Result result = new Result<>();
        result.setMsg("yes");
        return result;
    }

    /**
     * 修改采购量
     * @param diId
     * @param caigouliang
     * @param podiId
     * @return
     */
    @Override
    public Result updatecgl(Integer[] diId, Integer[] caigouliang, Integer podiId) {
        Result result = new Result();
        for (int i = 0; i < diId.length; i++) {
            podiPdMapper.updatecgl(podiId,diId[i],caigouliang[i]);
        }
        result.setMsg("成功");
        return result;
    }

}
